/* vim: set ts=2 et sw=2 cindent fo=qroca: */

package com.globant.google.mendoza.command;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.globant.google.mendoza.MendozaServer;
import com.globant.google.mendoza.MendozaRequest;
import com.globant.google.mendoza.MendozaServerState;

/** Represents the help mendoza server command.
 */
public final class MendozaHelpCommand extends MendozaBaseCommand {

  /** The class logger.
   */
  private static Log log = LogFactory.getLog(MendozaHelpCommand.class);

  /** Creates an instance of MendozaHelpCommand.
   *
   * @param mendozaRequest The mendoza server request.
   *
   * @param mendoza The mendoza server.
   *
   * @param mendozaState The mendoza server state.
   */
  public MendozaHelpCommand(
      final MendozaRequest mendozaRequest, final MendozaServer mendoza,
      final MendozaServerState mendozaState) {
    super(mendozaRequest, mendoza, mendozaState);
    setName("Help");
  }

  /** Executes the mendoza server reset command.
   */
  public void execute() {
    log.trace("Entering execute");
    String commandResultMsg = getHelpMessage();
    MendozaCommandResult result = new MendozaCommandResult();
    result.setSuccess(commandResultMsg);
    setResult(result);
    log.trace("Leaving execute");
  }

  /** Gets the help message.
   *
   * @return Returns the server help message.
   */
  private String getHelpMessage() {
    log.trace("Entering getHelpMessage");
    InputStream is =
      getClass().getResourceAsStream("/MendozaServerHTTPCommands.html");
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuffer sb = new StringBuffer();
    String line = null;
    try {
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
    } catch (IOException e) {
      log.trace("Leaving getHelpMessage", e);
      return "Mendoza internal server error. Cannot read help file";
    }
    log.trace("Leaving getHelpMessage");
    return sb.toString();
  }
}
